<template>
  <div class="componentsView">
    <el-menu
      class="el-menu-vertical-demo"
      default-active="TableDemo1"
      :default-openeds="['baseComponent', 'ElementPlus']"
      @open="handleOpen"
      @close="handleClose"
      @select="handleMenuSelect"
    >
      <el-sub-menu
        :index="item.value"
        v-for="(item, index) in state.subMenu"
        :key="item.value + item.title + index"
      >
        <!--  一级标题  -->
        <template #title>
          <el-icon>
            <location />
          </el-icon>
          <span>{{ item.title }}</span>
        </template>
        <el-sub-menu
          :index="itemChild.value"
          v-for="(itemChild, indexChild) in item.children"
          :key="itemChild.value + itemChild.title + indexChild"
        >
          <!--  二级标题  -->
          <template #title>{{ itemChild.childTitle }}</template>
          <!--  三级字段  -->
          <el-menu-item
            :index="val.value"
            v-for="(val, valIndex) in itemChild.childs"
            :key="val.value + val.label + valIndex"
          >
            {{ val.label }}
          </el-menu-item>
        </el-sub-menu>
      </el-sub-menu>
    </el-menu>

    <div v-if="state.currentTabComponent" class="component-box">
      <component
        v-bind:is="state.currentTabComponent"
        class="componentsCon"
        pieId="pieChartId2"
      ></component>
    </div>
  </div>
</template>

<script setup>
import { Location } from '@element-plus/icons-vue'
import { onMounted, reactive } from 'vue'
import stateData from './index'

const state = reactive({
  ...stateData // 解构数据
})

onMounted(() => {
  // 初始化默认渲染为TableDemo1组件
  state.currentTabComponent = state.tabComponents['TableDemo1']
})

/*
 * description sub-menu 展开的回调
 * param {key} <sub-menu> index的绑定值（:index）
 * param {keyPath} 打开的key对应的keyPath，以数组形式呈现
 * */
function handleOpen(key, keyPath) {
  console.log(key, 'index')
  // console.log(keyPath, 'indexPath')
}

/*
 * description sub-menu 收起的回调
 * param {key} <sub-menu> index的绑定值（:index）
 * param {keyPath} 收起的key对应的keyPath，以数组形式呈现
 * */
function handleClose(key, keyPath) {
  // console.log(key, 'index')
  // console.log(keyPath, 'indexPath')
}

/*
 * description sub-menu 菜单激活回调
 * param {key} <sub-menu-item> 选中菜单项的index的绑定值（:index）
 * param {keyPath} 选中菜单项的key对应的keyPath，以数组形式呈现
 * */
function handleMenuSelect(key, keyPath) {
  console.log(key, 'index')
  // console.log(keyPath, 'indexPath')

  state.currentTabComponent = state.tabComponents[key]

  if (key === 'TableDemo1') {
    state.tableDemo1.isShowForm = false
  }
  if (key === 'TableDemo2') {
    state.tableDemo2.isShowForm = false
  }
}
</script>

<style lang="scss" scoped>
.componentsView {
  width: 100%;
  height: calc(100% - 64px);
  display: flex;

  .el-menu-vertical-demo {
    width: 230px;
    height: 100%;
    max-height: 100%;
    // overflow-y: auto;
    text-align: left;
    // z-index: 99999;
  }

  .component-box {
    // background-color: #53a8ff;
    background: #f4f6fa;
    width: calc(100% - 230px);
    height: 100%;
    max-height: 100%;
    padding: 24px;
    box-sizing: border-box;
    .componentsCon {
      height: 100%;
      padding: 24px;
      box-sizing: border-box;
      background-color: #ffffff;
      border-radius: 5px;
      // overflow-y: auto;
    }
  }
}
</style>
